package edu.hsbremen.teamscore.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
import edu.hsbremen.teamscore.R;
import edu.hsbremen.teamscore.controller.ChangeIntOnObject;
import edu.hsbremen.teamscore.events.SoccerBreakObserver;
import edu.hsbremen.teamscore.events.TimerObserver;
import edu.hsbremen.teamscore.ui.UiCountdownTimer;

public class ScreenSoccer extends Activity {
	
	Button cornerTeam1;
	Button cornerTeam2;
	Button scoreTeam1;
	Button scoreTeam2;
	Button startStop;
	TextView team1Name;
	TextView team2Name;
	Chronometer mChrono;
	boolean counterIsRunning;
	boolean runfirst = true;
	long elapsedTimeBeforePause = 0;
	UiCountdownTimer countdown;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.soccer);
		
		counterIsRunning = false;
	
		cornerTeam1 = (Button) findViewById(R.id.soccer_corner_team1);
		cornerTeam2 = (Button) findViewById(R.id.soccer_corner_team2);
		scoreTeam1 = (Button) findViewById(R.id.soccer_score_team1);
		scoreTeam2 = (Button) findViewById(R.id.soccer_score_team2);
		startStop = (Button) findViewById(R.id.soccer_startstop);
		
		team1Name = (TextView) findViewById(R.id.soccer_txt_team1);
		team2Name = (TextView) findViewById(R.id.soccer_txt_team2);
		
		TimerObserver to = new SoccerBreakObserver(60, this);
		countdown = (UiCountdownTimer) findViewById(R.id.uiCountdownTimer);		
		countdown.addTimerEvent(to);
		
		Bundle namesBundle = getIntent().getExtras();
		team1Name.setText(namesBundle.getString("team1"));
		team2Name.setText(namesBundle.getString("team2"));
		
		scoreTeam1.setText("0");
		scoreTeam2.setText("0");
		cornerTeam1.setText("0");
		cornerTeam2.setText("0");
		
		// Listeners for all Buttons
		
		startStop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (counterIsRunning == true){
					startStop.setBackgroundResource(R.drawable.start1);
		        	countdown.stopCountdown();
//		        	Log.d("counter status: ", countdown.getCurrent() );
					counterIsRunning = false;
				}else {
					startStop.setBackgroundResource(R.drawable.stop1);
		            countdown.startCountdown();
					counterIsRunning = true;
				}
			}
		});
		
		scoreTeam1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				scoreTeam1.setText(new ChangeIntOnObject().countUp(scoreTeam1));
			}
		});
		scoreTeam1.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				scoreTeam1.setText(new ChangeIntOnObject().countDown(scoreTeam1));				
				return true;
			}
		});
		
		scoreTeam2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				scoreTeam2.setText(new ChangeIntOnObject().countUp(scoreTeam2));
			}
		});
		scoreTeam2.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				scoreTeam2.setText(new ChangeIntOnObject().countDown(scoreTeam2));				
				return true;
			}
		});
		
		cornerTeam1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cornerTeam1.setText(new ChangeIntOnObject().countUp(cornerTeam1));				
			}
		});
		cornerTeam1.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				cornerTeam1.setText(new ChangeIntOnObject().countDown(cornerTeam1));					
				return true;
			}
		});

		cornerTeam2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cornerTeam2.setText(new ChangeIntOnObject().countUp(cornerTeam2));				
			}
		});
		cornerTeam2.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				cornerTeam2.setText(new ChangeIntOnObject().countDown(cornerTeam2));					
				return true;
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu_soccer, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.soccer_rules:
	    	Intent Rules = new Intent(ScreenSoccer.this, Descriptions.class);
	    	Rules.putExtra("game", "soccer");
	    	startActivity(Rules);
	    	return true;
	    case R.id.soccer_backtomain:
	    	this.finish();
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}
